import Component, { mixins } from 'vue-class-component'
import { namespace } from 'vuex-class'

import DialogBoxMixin from '@/mixins/dialog'

import { loading, errorMessage } from '@/utils'

const companyUserModule = namespace('cms/companyUser')
const companyBasicModule = namespace('cms/companyBasic')
const companyJobModule = namespace('cms/companyJob')
const resumeSendModule = namespace('cms/resumeSend')
const interviewModule = namespace('cms/interview')

@Component
export default class Index extends mixins(DialogBoxMixin) {
  // Vuex
  @companyUserModule.Action('show') companyUserShow!: (payload: any) => Promise<any>
  @companyBasicModule.Action('show') companyBasicShow!: (payload: any) => Promise<any>

  @companyJobModule.Action('show') companyJobShow!: (payload: any) => Promise<any>
  @companyJobModule.Action getCompanyJobUser!: (payload: any) => Promise<any>

  @resumeSendModule.Action getResumeToSend!: (payload: any) => Promise<any>
  @resumeSendModule.Action getCompanyUserReceiveResumeSend!: (payload: any) => Promise<any>
  @resumeSendModule.Action getCompanyJobReceiveResumeSend!: (payload: any) => Promise<any>

  @interviewModule.Action getCompanyUserInterview!: (payload: any) => Promise<any>
  @interviewModule.Action getCompanyJobInterview!: (payload: any) => Promise<any>

  /**
   * 对话框：查看企业注册会员详情
   *
   * @param {number} id
   * @param {number} index
   * @return {void}
   */
  public dialogCompanyUserShow(id: number, index: number = -1): void {
    const _loading = loading({background: 'transparent'})
    this.companyUserShow({id}).then((res) => {
      _loading.close()
      if (res.status === 200) {
        this.dialogBox.width = '1000px'
        this.dialogBox.component = 'company-user-show'
        this.$nextTick(() => {
          this.dialogBox.visible = true
          this.dialogBox.index = index
        })
      } else {
        errorMessage(res.message)
      }
    }).catch((err) => {
      _loading.close()
    })
  }

  /**
   * 对话框：查看企业基本详情
   *
   * @param {number} id
   * @param {number} index
   * @return {void}
   */
  public dialogCompanyBasicShow(id: number, index: number = -1): void {
    const _loading = loading({background: 'transparent'})
    this.companyBasicShow({id}).then((res) => {
      _loading.close()
      if (res.status === 200) {
        this.dialogBox.width = '1000px'
        this.dialogBox.component = 'company-basic-show'
        this.$nextTick(() => {
          this.dialogBox.visible = true
          this.dialogBox.index = index
        })
      } else {
        errorMessage(res.message)
      }
    }).catch((err) => {
      _loading.close()
    })
  }

  /**
   * 对话框：查看招聘岗位详情
   *
   * @param {number} id
   * @param {number} index
   * @return {void}
   */
  public dialogCompanyJobShow(id: number, index: number = -1): void {
    const _loading = loading({background: 'transparent'})
    this.companyJobShow({id}).then((res) => {
      _loading.close()
      if (res.status === 200) {
        this.dialogBox.width = '1000px'
        this.dialogBox.component = 'company-job-show'
        this.$nextTick(() => {
          this.dialogBox.visible = true
          this.dialogBox.index = index
        })
      } else {
        errorMessage(res.message)
      }
    }).catch(() => {
      _loading.close()
    })
  }

  /**
   * 对话框：查看企业招聘职位
   *
   * @param {number} uid
   * @param {number} [index]
   * @return {void}
   */
  public dialogCompanyJobUser(uid: number, index: number = -1): void {
    const _loading = loading({background: 'transparent'})
    this.getCompanyJobUser({uid}).then((res) => {
      _loading.close()
      if (res.status === 200) {
        this.dialogBox.width = '1100px'
        this.dialogBox.component = 'company-job-user'
        this.$nextTick(() => {
          this.dialogBox.visible = true
          this.dialogBox.index = index
        })
      } else {
        errorMessage(res.message)
      }
    }).catch(() => {
      _loading.close()
    })
  }

  /**
   * 对话框：企业用户收到简历投递
   *
   * @param {Number} uid
   * @param {Object} [params]
   * @return {void}
   */
  public dialogCompanyUserReceiveResumeSend(uid: number, params: any = {}) {
    const _loading = loading({background: 'transparent'})
    this.getCompanyUserReceiveResumeSend({uid, params}).then((res) => {
      _loading.close()
      if (res.status === 200) {
        this.dialogBox.width = '1100px'
        this.dialogBox.component = 'company-user-receive-resume-send'
        this.$nextTick(() => {
          this.dialogBox.visible = true
        })
      } else {
        errorMessage(res.message)
      }
    }).catch((err) => {
      _loading.close()
    })
  }

  /**
   * 对话框：企业用户收到简历投递
   *
   * @param {Number} id
   * @param {Object} [params]
   * @return {void}
   */
  public dialogCompanyJobReceiveResumeSend(id: number, params: any = {}) {
    const _loading = loading({background: 'transparent'})
    this.getCompanyJobReceiveResumeSend({id, params}).then((res) => {
      _loading.close()
      if (res.status === 200) {
        this.dialogBox.width = '1100px'
        this.dialogBox.component = 'company-job-receive-resume-send'
        this.$nextTick(() => {
          this.dialogBox.visible = true
        })
      } else {
        errorMessage(res.message)
      }
    }).catch((err) => {
      _loading.close()
    })
  }

  /**
   * 对话框：企业招聘收到简历投递
   *
   * @param {number} job_id
   * @param {object} [params]
   * @return {void}
   */
  public dialogCompanyJobReceivedResumeSend(job_id: number, params: any = {}): void {
    const _loading = loading({background: 'transparent'})
    this.getResumeToSend({job_id, params}).then((res) => {
      _loading.close()
      if (res.status === 200) {
        this.dialogBox.width = '1000px'
        this.dialogBox.component = 'resume-to-send'
        this.$nextTick(() => {
          this.dialogBox.visible = true
        })
      } else {
        errorMessage(res.message)
      }
    }).catch((err) => {
      _loading.close()
    })
  }

  /**
   * 对话框：企业用户面试通知
   *
   * @param {number} uid
   * @param {object} [params]
   * @return {void}
   */
  public dialogCompanyUserInterview(uid: number, params: any = {}): void {
    const _loading = loading({background: 'transparent'})
    this.getCompanyUserInterview({uid, params}).then((res) => {
      _loading.close()
      if (res.status === 200) {
        this.dialogBox.width = '1100px'
        this.dialogBox.component = 'company-user-interview'
        this.$nextTick(() => {
          this.dialogBox.visible = true
        })
      } else {
        errorMessage(res.message)
      }
    }).catch((err) => {
      _loading.close()
    })
  }

  /**
   * 对话框：企业招聘岗位面试通知
   *
   * @param {number} id
   * @param {object} [params]
   * @return {void}
   */
  public dialogCompanyJobInterview(id: number, params: any = {}): void {
    const _loading = loading({background: 'transparent'})
    this.getCompanyJobInterview({id, params}).then((res) => {
      _loading.close()
      if (res.status === 200) {
        this.dialogBox.width = '1100px'
        this.dialogBox.component = 'company-job-interview'
        this.$nextTick(() => {
          this.dialogBox.visible = true
        })
      } else {
        errorMessage(res.message)
      }
    }).catch((err) => {
      _loading.close()
    })
  }
}
